package com.fruit.servlet;

import com.fruit.pojo.Fruit;
import com.fruit.pojo.FruitCategory;
import com.fruit.service.FruitCategoryService;
import com.fruit.service.FruitService;
import com.fruit.service.impl.FruitCategoryServiceImpl;
import com.fruit.service.impl.FruitServiceImpl;
import com.fruit.util.ParameterUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class FruitServlet extends HttpServlet {

    private FruitCategoryService fruitCategoryService = new FruitCategoryServiceImpl();
    private FruitService fruitService = new FruitServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String act = request.getParameter("act");
        try {
            if ("go_add".equals(act)) {
                List<FruitCategory> categoryList = fruitCategoryService.getCategoryList();
                request.setAttribute("categoryList", categoryList);
                request.getRequestDispatcher("add.jsp").forward(request, response);
            } else if ("add".equals(act)) {
                String name = request.getParameter("name");
                String place = request.getParameter("place");
                Integer weight = ParameterUtil.getInteger(request, "weight");
                Integer categoryId = ParameterUtil.getInteger(request, "categoryId");
                Float price = ParameterUtil.getFloat(request, "price");//元

                Fruit fruit = new Fruit();
                fruit.setName(name);
                fruit.setPlace(place);
                fruit.setPrice(new Float(price * 100).intValue());
                fruit.setWeight(weight);
                FruitCategory fruitCategory = new FruitCategory();
                fruitCategory.setId(categoryId);
                fruit.setFruitCategory(fruitCategory);
                fruitService.addFruit(fruit);
                response.sendRedirect("fruit.html?act=list");
            } else if ("list".equals(act)) {
                Integer categoryId = ParameterUtil.getInteger(request, "categoryId");
                Integer id = ParameterUtil.getInteger(request, "id");
                String name = request.getParameter("name");
                Fruit fruit = new Fruit();
                fruit.setName(name);
                fruit.setId(id);
                FruitCategory fruitCategory = new FruitCategory();
                fruitCategory.setId(categoryId);
                fruit.setFruitCategory(fruitCategory);
                List<Fruit> fruitList = fruitService.getFruitList(fruit);
                request.setAttribute("fruitList", fruitList);
                List<FruitCategory> categoryList = fruitCategoryService.getCategoryList();
                request.setAttribute("categoryList", categoryList);
                request.getRequestDispatcher("list.jsp").forward(request, response);
            } else if ("del".equals(act)) {
                Integer id = ParameterUtil.getInteger(request, "id");
                fruitService.deleteFruit(id);
                response.sendRedirect("fruit.html?act=list");
            } else if ("detail".equals(act)) {
                Integer id = ParameterUtil.getInteger(request, "id");
                Fruit fruit = fruitService.getFruitById(id);
                request.setAttribute("fruit", fruit);
                request.getRequestDispatcher("detail.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error", e.getMessage());
            request.getRequestDispatcher("error.jsp").forward(request, response);
        }
    }
}
